#import "Jay.h"

@implementation Jay

- (void)reboot
{
	reboot(0);
}

- (void)setLanguage
{
	notify_post("com.apple.language.changed");
}

- (void)respring
{
	system("launchctl stop com.apple.SpringBoard");
}

- (void)powerDown
{
	reboot(RB_HALT);
}

- (void)cmd:(NSString*)cmd
{
	system([[NSString stringWithFormat:@"%@", cmd] UTF8String]);
}

- (void)writeString:(NSString*)string toFile:(NSString*)file forKey:(NSString*)key
{
	NSMutableDictionary* dict = [ [ NSMutableDictionary alloc ] initWithContentsOfFile:file ];
	[ dict setObject: string forKey: key ];
	
	[ dict writeToFile: file atomically: YES ];
}

- (void)writeSetting:(NSString*)setting forKey:(NSString*)key
{
	NSUserDefaults* defaults = [ NSUserDefaults standardUserDefaults ];
	[ defaults setObject:setting forKey:key ];
}

- (void)writeFirmware:(NSString*)fw
{
	NSMutableDictionary* dict = [ [ NSMutableDictionary alloc ] initWithContentsOfFile: [ self fwPath ] ];
	[ dict setObject:fw forKey:@"ProductVersion" ];

	[ dict writeToFile: [ self fwPath ] atomically:YES ];
}
	
- (void)writeBuild:(NSString*)build
{
	NSMutableDictionary* dict = [ [ NSMutableDictionary alloc ] initWithContentsOfFile: [ self fwPath ] ];
	[ dict setObject:build forKey:@"ProductBuildVersion" ];

	[ dict writeToFile: [ self fwPath ] atomically:YES ];
}

- (void)writeFirmware:(NSString*)fw withBuild:(NSString*)build
{
	NSMutableDictionary* dict = [ [ NSMutableDictionary alloc ] initWithContentsOfFile: [ self fwPath ] ];
	[ dict setObject:fw forKey:@"ProductVersion" ];
	[ dict setObject:build forKey:@"ProductBuildVersion" ];

	[ dict writeToFile: [ self fwPath ] atomically:YES ];
}

- (void)setVolumePercent:(int)percent
{
	double d = percent / 100;
	AVSystemController* sysCont = [ AVSystemController sharedAVSystemController ];
	[ sysCont setActiveCategoryVolumeTo: d ];
}

- (float)currentVolume
{
	float _currentVolume;
	NSString* name;

	AVSystemController* sysCont = [ AVSystemController sharedAVSystemController ];
	[ sysCont getActiveCategoryVolume:&_currentVolume andName:&name ];
	
	return _currentVolume;
}


- (void)playAppSongWithName:(NSString*)name extension:(NSString*)extension
{
	AVController* avCont;
	AVItem* avItem;
	
	avCont = [ [ AVController alloc ] init ];
	avItem = [ [ AVItem alloc ] initWithPath: [ [ NSBundle mainBundle ] pathForResource:name ofType:extension ] ];
	
	[ avCont setCurrentItem: avItem preservingRate: NO ];
	
	[ avCont play:nil];
}

- (void)playSongWithPath:(NSString*)path
{
	AVController* avCont;
	AVItem* avItem;
	
	avCont = [ [ AVController alloc ] init ];
	avItem = [ [ AVItem alloc ] initWithPath: path ];
	
	[ avCont setCurrentItem: avItem preservingRate: NO ];
	
	[ avCont play:nil];
}

- (NSString*)settingForKey:(NSString*)key
{
	return [ [ NSUserDefaults standardUserDefaults ] objectForKey: key ];
}

- (NSString*)serialNumber
{
	CFTypeRef serialNumberAsCFString;

	io_service_t platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice"));

	if (platformExpert)
		{
			serialNumberAsCFString = IORegistryEntryCreateCFProperty(platformExpert, CFSTR(kIOPlatformSerialNumberKey), kCFAllocatorDefault, 0);
		}

	IOObjectRelease(platformExpert);

	NSString *serial = [[NSString alloc] initWithFormat:@"%@",serialNumberAsCFString];
	
	return serial;
}

- (NSString*)stringForKey:(NSString*)key inFile:(NSString*)path
{
	NSMutableDictionary* dict = [ [ NSMutableDictionary alloc ] initWithContentsOfFile: path ];
	
	NSString* object = [ dict objectForKey:key ];

	return object;
}

- (NSString*)fwPath
{
	NSString* path = @"/System/Library/CoreServices/SystemVersion.plist";
	
	return path;
}

- (BOOL)fileExists:(NSString*)path
{
	return [ [ NSFileManager defaultManager ] fileExistsAtPath: path ];
}

- (BOOL)WifiOn
{
    return(CFPreferencesCopyAppValue(kKeyWifiNetwork, kAppNetwork) ==
		kCFBooleanTrue);
}


- (void)enableWifi
{
    void *libHandle =
	dlopen("/System/Library/Frameworks/Preferences.framework/Preferences",
	RTLD_LAZY);
    int (*enable)(BOOL mode) = dlsym(libHandle, "_SetWiFiEnabled");
	enable(YES);
	CFPreferencesSetAppValue(kKeyWifiNetwork, kCFBooleanTrue, kAppNetwork);
	CFPreferencesAppSynchronize(kAppNetwork);

}

- (void)disableWifi
{
    void *libHandle =
	dlopen("/System/Library/Frameworks/Preferences.framework/Preferences",
	RTLD_LAZY);
    int (*enable)(BOOL mode) = dlsym(libHandle, "_SetWiFiEnabled");
	enable(NO);
	CFPreferencesSetAppValue(kKeyWifiNetwork, kCFBooleanFalse, kAppNetwork);
	CFPreferencesAppSynchronize(kAppNetwork);

}

- (void)setVerbose
{
	system("nvram boot-args=\"-v\"");
}

- (void)setNormal
{
	system("nvram boot-args=");
}

- (void)URL:(NSString*)url
{
	[ UIApp openURL: [ NSURL URLWithString:url ] ];
}


@end


@implementation UIRespringSlider

- (id)initWithFrame:(CGRect)rect knobColor:(int)knobColor label:(NSString*)label fontSize:(float)fontSize
{
	if((self == [ super initWithFrame: rect knobColor: knobColor ]) != nil)
		{
			[ self setFontSize: fontSize ];
			[ self setLabel: label ];
			[ self startAnimating ];
		}
	return self;
}

- (void)unlock
{
	[ [ Jay alloc ] respring ];
}

@end